package servlet;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import factory.DAOFactory;
import test.MD5;
import vo.Admin;
import vo.TicketIn;


public class AdminServlet extends HttpServlet {
    public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException {
    	this.doPost(req, resp);
    }
    
    public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
    	String path = null;
    	String status = req.getParameter("status");
    	if(status!=null) {
    		if("login".equals(status)) {
		    	String id = req.getParameter("id");
		    	String password = req.getParameter("password");
//		    	try {
//					password=MD5.generateCode(password);
//				} catch (Exception e1) {
//					e1.printStackTrace();
//				}
		    	Admin admin = new Admin();
		    	admin.setAdminId(id);
		    	admin.setAdminPwd(password);
		    	try {
					if(DAOFactory.getAdminDaoInstance().login(admin)) {
						req.getSession().setAttribute("uname", admin.getAdminName());
						req.getSession().setAttribute("leval", admin.getAdminLeval());
						path = "AdminCtrl.jsp";
					}
					else {
						ServletContext context = this.getServletContext();
						context.setAttribute("err", "login failed,username or password is not Correct");
						path="admin_login.jsp";
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
    		}
    		
    		if("addAdmin".equals(status)) {
    			boolean flag = false;
    			String id = req.getParameter("id");
		    	String password = req.getParameter("password");
		    	String name = req.getParameter("name");
		    	int leval = Integer.valueOf(req.getParameter("leval"));
		    	try {
//					password = MD5.generateCode(password);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
		    	Admin admin = new Admin();
		    	admin.setAdminId(id);
		    	admin.setAdminPwd(password);
		    	admin.setAdminName(name);
		    	admin.setAdminLeval(leval);
		    	try {
		    		DAOFactory.getAdminDaoInstance().insert(admin);
		    		flag = true;
		    	}catch(Exception e) {
		           e.printStackTrace();
		    	}
		    	req.setAttribute("flag", flag);
		    	path="adminlogin_do.jsp";
    		}
    		
    		if("modifyT".equals(status)) {
    			boolean flag = false;
    			String tid = req.getParameter("tId");
		        int carr = Integer.valueOf(req.getParameter("carr"));
		        String seatn = req.getParameter("seatN");
		        float price = Float.parseFloat(req.getParameter("price"));
		    	TicketIn tk = new TicketIn();
		    	tk.setTId(tid);
		    	tk.setCarr(carr);
		    	tk.setSeatN(seatn);
		    	tk.setPrice(price);
		    	try {
		    		DAOFactory.getAdminDaoInstance().modifyT(tk);
		    		flag = true;
		    	}catch(Exception e) {
		           e.printStackTrace();
		    	}
		    	req.setAttribute("flag", flag);
		    	path="Modify_do.jsp";
    		}
    		
    		if("selectUAll".equals(status)) {
		    	try {
		    		req.setAttribute("all", DAOFactory.getAdminDaoInstance().selectPAll());
		    	}catch(Exception e) {
		           e.printStackTrace();
		    	}
		    	path="userInfo.jsp";
    		}
    		
    		if("deleteU".equals(status)) {
    			int leval = (Integer)req.getSession().getAttribute("leval");
    			if(leval<=2) {
    			boolean flag = false;
    			String userId = req.getParameter("userId");
		    	try {
		    		DAOFactory.getAdminDaoInstance().deleteP(userId);
		    		flag = true;
		    	}catch(Exception e) {
		           e.printStackTrace();
		    	}
		    	req.setAttribute("flag", flag);
		    	   path="deleteU_do.jsp";
    			}else{
    			   path="delete_do.jsp";
    			}
    		}
    			
    		}else {
    			path = "error01.jsp";
    		}
    		
		    req.getRequestDispatcher(path).forward(req, resp);
    }
}
